<?php
require_once "conf.php";
require_once "www/_include/smarty/libs/Smarty.class.php";
require_once "services/FacebookProxy.php";
require_once "application/user/UserUtils.php";
require_once "data/dao/UserDAO.php";
require_once "data/dao/CollectionDAO.php";
require_once "data/dao/GameDAO.php";
require_once "data/dao/WishListDAO.php";
require_once "data/dao/FriendCodeDAO.php";
require_once "application/game/OwnedGame.php";
require_once "utils/ClassUtils.php";
require_once "www/CodeHandler.php";
require_once "Constants.php";
require_once "utils/DateUtils.php";

class DashRenderer{
	
	private $user;
	private $smarty;
	private $numberOfGames;
	private $numberOfGamesInWishList;
		
	public function DashRenderer(){
		
		$this->smarty = new Smarty();
		$facebook = new FacebookProxy();
		$facebook->requireFrame();

		$udao = new UserDAO();
		$userId = $facebook->requireLogin();

		if(!UserUtils::userExists($userId)){
			$udao->addUser($userId);
		}

		$this->user = $udao->findUser($userId); 
		
		$this->numberOfGames = 0;
		$this->numberOfGamesInWishList = 0;
		
		// if there are any user errors render message
		if($_REQUEST['error'] != null){
			$this->renderErrorWarnings($_REQUEST['error']);
		}
		
		$facebook->updateProfile();
		
		$this->getPlaceholderValues();
		$this->renderPage();	
	}
	
	private function getPlaceholderValues(){
		
		$firstname = $this->user->getFirstName();
		$lastname = $this->user->getLastName();
		 
		$this->smarty->assign("userName", $firstname . " " . $lastname);
		$this->smarty->assign("wiiCode", $this->user->getWiiCode());
		$this->smarty->assign("collection", $this->getCollection());
		$this->smarty->assign("wishlist", $this->getWishList());
		$this->smarty->assign("numberOfGames", $this->numberOfGames);
		$this->smarty->assign("numberOfGamesInWishList", $this->numberOfGamesInWishList);
		$this->smarty->assign("styleSheet", file_get_contents("./html/wiiSocial.css"));
		$this->smarty->assign("javaScript", file_get_contents("./html/wiiSocial.js"));
		$this->smarty->assign("HOST", HOST_URL);
		$this->smarty->assign("user", $this->user);

		$displayPic = $this->user->getProfilePicBigUrl(); 
		// this is the facebook '?'
		if ($displayPic == null){
			$displayPic = "http://static.ak.fbcdn.net/pics/q_default.gif";
		}
		
		$this->smarty->assign("displayPic", $displayPic);
		
		$location = $this->user->getLocationISO3166A2();
		$this->smarty->assign("userLocation", $location);
		$this->smarty->assign("IP", $_SERVER["REMOTE_ADDR"]);
		
		$upcomingReleases = $this->getUpcomingReleases($this->user->getStoreLocation());
		$this->smarty->assign("thisMonthsReleases", $upcomingReleases[0]);
		$this->smarty->assign("nextMonthsReleases", $upcomingReleases[1]);
		
		$this->smarty->assign("thisMonth", DateUtils::getMonthAsString(date("m")));
		$this->smarty->assign("nextMonth", DateUtils::getNextMonthAsString(date("m")));
		
		$renderInfo = false;
		if ($this->user->getLocationISO3166A2() == null || $this->user->getStoreLocation() == null){
			$renderInfo = true;
		}
		$this->smarty->assign("renderInfo", $renderInfo);
	}
	
	private function renderPage(){
		
		$this->smarty->display("index.tpl");
	}
	
	private function renderErrorWarnings($m){

		$this->smarty->assign("errors", true);
		$this->smarty->assign("message", $m);
	}

	public function getCollection(){
	
		$cdao = new CollectionDAO();
		$collection = $cdao->getCollection($this->user->getUserId());
		
		if($collection == null)
			return;
			
		$gdao = new GameDAO();
		$games = array();
		
		foreach($collection->getGames() as $g){
			$ownedGame = ClassUtils::typecast($gdao->getGame($g), "OwnedGame");
			
			if ($ownedGame != null)
				$ownedGame->setFriendCode($this->getFriendCode($g));
			
			array_push($games, $ownedGame);
		}
		
		$this->numberOfGames = count($games);
		return $games;
	}
	
	public function getFriendCode($gameID){
		
		$fcdao = new FriendCodeDAO();
		$friendCode = $fcdao->findFriendCode($this->user->getUserId(), $gameID);
		if ($friendCode == null)
			return false;
			
		return $friendCode;
	}

	public function getWishList(){
	
		$wldao = new WishListDAO();
		$wishList = $wldao->getWishList($this->user->getUserId());
		
		if($wishList == null)
			return null;
			
		$gdao = new GameDAO();
		$games = array();
		
		foreach($wishList as $g){
			$game = $gdao->getGame($g);
			array_push($games, $game);
		}
		$this->numberOfGamesInWishList = count($games);
		return $games;
	}
	
	
	public function getNumberOfGames(){
		return $this->numberOfGames;
	}
	
	public function getNumberOfGamesInWishList(){
		return $this->numberOfGamesInWishList;
	}
	
	public function getUpcomingReleases($locale){
		
		$yearNow = date("Y");
		$monthNow = date("m");
		$dayNow = date("d");
		
		$thisMonth = array();
		$nextMonth = array();
		
		$gdao = new GameDAO();
		foreach ($gdao->getAllGames($locale) as $g){
			
			$explodedDate = explode("-", $g->getReleaseDate());
			$day = $explodedDate[2];
			$month = $explodedDate[1];
			$year = $explodedDate[0];

			if($year == $yearNow){
				
				if($month == $monthNow && $day > $dayNow){
					array_push($thisMonth, $g);
				}
				
				if($month == $monthNow+1){
					array_push($nextMonth, $g);
				}
				
				
			}
		}
		
		return array($thisMonth, $nextMonth);
	}
	
}
$dr = new DashRenderer(); 
?>